package com.tiny.mirror.service.product.interfaces.controller;

import com.api.tiny.mirror.service.product.AppApiService;
import com.api.tiny.mirror.service.product.request.CreateAppRequest;
import com.api.tiny.mirror.service.product.request.PageAppRequest;
import com.api.tiny.mirror.service.product.request.UpdateAppRequest;
import com.api.tiny.mirror.service.product.response.AppVO;
import com.tiny.mirror.basic.log.annotation.LogBody;
import com.tiny.mirror.basic.web.dto.response.BasePageResponse;
import com.tiny.mirror.basic.web.dto.response.BaseResponse;
import com.tiny.mirror.service.product.application.command.AppCmdService;
import com.tiny.mirror.service.product.application.query.AppQryService;
import java.util.Arrays;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: liujsh
 * @Description:
 * @Date: Create in 2023/9/25
 */
@RestController
public class AppController implements AppApiService {

  @Resource
  private AppQryService appQryService;
  @Resource
  private AppCmdService appCmdService;

  @LogBody
  @Override
  public BasePageResponse<AppVO> page(@RequestBody @Validated PageAppRequest request) {
    return appQryService.page(request);
  }

  @Override
  public BaseResponse<AppVO> getInfo(@PathVariable(value = "id") Long id) {
    return BaseResponse.success(appQryService.getInfo(id));
  }

  @LogBody
  @Override
  public BaseResponse<Long> create(@RequestBody @Validated CreateAppRequest request) {
    // 同类型只能创建一个
    appQryService.checkCreateApp(request.getType());
    return BaseResponse.success(appCmdService.create(request));
  }

  @LogBody
  @Override
  public BaseResponse<Void> update(@RequestBody @Validated UpdateAppRequest request) {
    appCmdService.update(request);
    return BaseResponse.success();
  }

  @Override
  public BaseResponse<Integer> delete(@PathVariable(value = "ids") Long[] ids) {
    appCmdService.delete(Arrays.asList(ids));
    return BaseResponse.success();
  }
}
